TypeScript هي لغة برمجة مفتوحة المصدر طورتها Microsoft، وهي تعتبر امتدادًا لـ JavaScript مع إضافة ميزات مثل التنقيط الثابت (Static Typing) والبرمجة الكائنية (OOP) بشكل أقوى. TypeScript يتم تحويله إلى JavaScript حتى يعمل في المتصفحات وبيئات Node.js.
npm install -g typescript
tsc app.ts
let age: number = 25;
let name: string = "John";
let isActive: boolean = true;
let list: number[] = [1, 2, 3];
let person: { name: string, age: number } = { name: "John", age: 25 };
let userName: string = "Alice";
let userAge: number = 30;
let isStudent: boolean = false;
console.log(`Name: ${userName}, Age: ${userAge}, Student: ${isStudent}`);
function add(x: number, y: number): number {
return x + y;
}
let result = add(5, 10);
console.log(result); // Output: 15
const multiply = (x: number, y: number): number => x * y;
console.log(multiply(3, 4)); // Output: 12
interface Person {
name: string;
age: number;
isStudent?: boolean; // Optional property
}
let user: Person = {
name: "Alice",
age: 25
};
console.log(user.name); // Output: Alice
class Animal {
name: string;
constructor(name: string) {
this.name = name;
}
speak(): void {
console.log(`${this.name} makes a noise.`);
}
}
class Dog extends Animal {
breed: string;
constructor(name: string, breed: string) {
super(name);
this.breed = breed;
}
speak(): void {
console.log(`${this.name} barks.`);
}
}
let dog = new Dog("Buddy", "Golden Retriever");
dog.speak(); // Output: Buddy barks.
function identity(arg: T): T {
return arg;
}
let output1 = identity("Hello");
let output2 = identity(42);
console.log(output1); // Output: Hello
console.log(output2); // Output: 42
// math.ts
export function add(x: number, y: number): number {
return x + y;
}
// app.ts
import { add } from './math';
let result = add(5, 10);
console.log(result); // Output: 15